﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("Text"), ToolboxData("<{0}:BannerAd runat=server></{0}:BannerAd>")> _
Public Class BannerAd
    Inherits WebControl

    ''' <summary>
    ''' Default text property of a server control
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property Text() As String
        Get
            Dim s As String = CStr(ViewState("Text"))
            If s Is Nothing Then
                Return "[" + Me.ID + "]"
            Else
                Return s
            End If
        End Get

        Set(ByVal Value As String)
            ViewState("Text") = Value
        End Set
    End Property

    ''' <summary>
    ''' For rendering any contents (HTML)
    ''' </summary>
    ''' <param name="output"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
        MyBase.RenderContents(output)

    End Sub

    ''' <summary>
    ''' Create child controls in the server control
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub CreateChildControls()
        MyBase.CreateChildControls()

        ' Create a new adrotator
        Dim adRotator As New AdRotator

        AddHandler adRotator.AdCreated, AddressOf AdRotator_AdCreated

        ' Add the control to the output
        Me.Controls.Add(adRotator)

    End Sub

    ''' <summary>
    ''' Will fire on the event of the BannerRotator adding 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub AdRotator_AdCreated(ByVal sender As Object, ByVal e As AdCreatedEventArgs)
        Try
            ' Get Manager
            Dim manager As New DotNetCore.BannerManagement.BLL.AdvertisementManager

            Dim advertisement As DotNetCore.BannerManagement.Domain.dnc_Advertisement = manager.GetNextAdvertisement(Zone)

            Dim installDirectory As String = My.Settings.InstallDirectory

            ' Assign advertisement properties to ad rotator
            e.AlternateText = advertisement.AlternateText
            e.ImageUrl = installDirectory & "/Advertisement/LoadBanner.aspx?ID=" & advertisement.ID.ToString
            e.NavigateUrl = installDirectory & "/Advertisement/ClickThrough.aspx?ID=" & advertisement.ID.ToString
        Catch
        End Try
    End Sub

    Private _zoneName As String = "Main"
    ''' <summary>
    ''' A property set on the server control
    ''' It dictates what zone to display an advertisement from will go
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Zone() As String
        Get
            Return _zoneName
        End Get
        Set(ByVal value As String)
            _zoneName = value
        End Set
    End Property

End Class
